package com.tom.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;

import com.tom.util.HttpRequest;
import com.tom.util.JDBCUtil;

public class ZrbBussinessData {

	public static void main(String[] args) {
		try {
			ArrayList<String> sqlList = new ArrayList<String>();
			ArrayList<String> urList = JDBCUtil.getUrls("qss_hairongyi_url");
			String url = "";
			String cookie = "UniqueName=9ff5222a-aa9a-ad1e-d94a-23d0c6d785fa; hr_cookieId=2900db43b4ea461289345bbf6c62df2d; hr_firstTime=1525246315; p2p_guid=caead968-5c95-471b-b9ac-207a81fec5f4; _fmdata=4F5327074AD6B6D76B79FDEC523FED0F10721F0844645295CB803084EFB3A0535C3E593A5DDAEFDB2596CE617281F8B903304D29AE2E24C9; hr_active_value={\"hairongyi.com\":{\"l\":\"2018/6/1\",\"t\":1}}; hr_utmz=1527828399|bdpc||guanwang|||; ZXKJSESSIONID=5541710a-a503-46f9-0a90-7ddb7ccbe722***2; 136a3d03-9748-4f83-a54f-9b2a93f979a0=c252beaa-021e-4c9e-9bf4-7957268df572; Hm_lvt_c64c9bf97a311ea8df0c43e70b4dbaa8=1526438625,1527828400,1527839739; hr_sessionId=181ab178109e4a08b216307786b97837; loginname=15092200806; com.vfsso.cas.token=e232a9825ff949208e3e3ae7bc172f00; Hm_lpvt_c64c9bf97a311ea8df0c43e70b4dbaa8=1527842736";
			for (int i = 0; i < urList.size(); i++) {
				url = urList.get(i);
				String res = HttpRequest.sendGetByCookie(url, cookie);
				res = res.split("</body>")[0].split("<body>")[1];
				res = res.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "#");
				// System.err.println(res);
				String[] name = res.split("#");
				String item_name = null;
				String item_bumber = null;
				String publish_date = null;
				String year_rate = null;
				String loan_days = null;
				String loan_amt = null;
				String interest_date = null;
				String income_mehtod = null;
				String loan_name = null;
				String item_area = null;
				String repay_date = null;
				String end_time = null;
				String amt_use = null;
				String repay_source = null;
				String repay_guarantee = null;
				String item_desc = null;
				String registDay = null;
				String registCapital = null;
				String industry = null;
				String credit = null;
				String operationScope = null;
				String otherLoanInfo = null;
				if (name[197].isEmpty()) {
					item_name = name[168];
					item_bumber = name[174];
					publish_date = name[178];
					year_rate = name[202] + name[204];
					loan_days = name[216] + name[218];
					loan_amt = name[244];
					interest_date = name[250];
					income_mehtod = name[262];
					loan_name = name[332];
					item_area = name[344];
					repay_date = name[357];
					end_time = name[410];
					amt_use = name[422];
					repay_source = name[434];
					repay_guarantee = name[446];
					item_desc = name[459];
					registDay = name[494];
					registCapital = name[506];
					industry = name[518];
					credit = name[530];
					operationScope = name[542];
					otherLoanInfo = name[554];
				} else {
					item_name = name[168];
					item_bumber = name[174];
					publish_date = name[178];
					year_rate = name[204] + name[206];
					loan_days = name[218] + name[220];
					loan_amt = name[246];
					interest_date = name[252];
					income_mehtod = name[264];
					loan_name = name[334];
					item_area = name[346];
					repay_date = name[359];
					end_time = name[412];
					amt_use = name[424];
					repay_source = name[436];
					repay_guarantee = name[448];
					item_desc = name[463] + name[464] + name[465] + name[466] + name[467] + name[468] + name[469]
							+ name[470] + name[471] + name[472] + name[473] + name[477] + name[480] + name[481]
							+ name[484] + name[485];
					registDay = name[538];
					registCapital = name[550];
					industry = name[562];
					credit = name[574];
					operationScope = name[586];
					otherLoanInfo = name[595];
				}
				Date now = new Date();
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				String time = dateFormat.format(now);
				String sql = "insert into qss_hairongyi_bussiness_data(item_name,item_bumber,publish_date,year_rate,loan_days,loan_amt,interest_date,income_mehtod,loan_name,item_area,repay_date,end_time,amt_use"
						+ ",repay_source,repay_guarantee,item_desc,registDay,registCapital,industry,credit,operationScope,otherLoanInfo,insert_date,url)values("
						+ "" + "'" + item_name + "'" + "," + "'" + item_bumber + "'" + "," + "'" + publish_date + "'"
						+ "," + "'" + year_rate + "'" + "," + "'" + loan_days + "'" + "," + "'" + loan_amt + "'" + ""+","
						+ "" + "'" + interest_date + "'" + "," + "'" + income_mehtod + "'" + "," + "'" + loan_name + "'"
						+ "," + "'" + item_area + "'" + "," + "'" + repay_date + "'" + "," + "'" + end_time + "'" + ""
						+ "," + "'" + amt_use + "'" + "," + "'" + repay_source + "'" + "," + "'" + repay_guarantee + "'"
						+ "," + "'" + item_desc + "'" + "," + "'" + registDay + "'" + "," + "'" + registCapital + "'"
						+ "" + "," + "'" + industry + "'" + "," + "'" + credit + "'" + "," + "'" + operationScope + "'"
						+ "," + "'" + otherLoanInfo + "'"+ "," + "'" + time + "'"+ "," + "'" + url + "'" + ")";
				System.out.println(sql);
				sqlList.add(sql);
			}
			JDBCUtil.insertData(sqlList);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
